<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="泊位编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入泊位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="diy-mb-5">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['admin:berth:create']">新增车位</el-button>
    </div>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column
      v-for="(item,index) in tableList"
      :key="index"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'status'" :options="dict.type.EBerthStatus" :value="row[item.prop]"/>
          <dict-tag
          v-else-if="item.prop == 'pushFlag' || item.prop == 'hiddenFlag' || item.prop == 'offlineFlag' || item.prop == 'abnormalFlag'" 
          :options="dict.type.EYesOrNo" :value="row[item.prop]"/>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:berth:update']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            @click="handleOnline(scope.row)"
            v-hasPermi="['admin:berth:setOffline']"
            v-if="scope.row.offlineFlag == 0"
          >下线
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleOnline(scope.row)"
            v-hasPermi="['admin:berth:setOnline']"
            v-if="scope.row.offlineFlag == 1"
          >上线
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            @click="handleHide(scope.row)"
            v-hasPermi="['admin:berth:hidden']"
            v-if="scope.row.hiddenFlag == 0"
          >隐藏
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="handleHide(scope.row)"
            v-hasPermi="['admin:berth:show']"
            v-if="scope.row.hiddenFlag == 1"
          >显示
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="departmentId">
              <el-select :disabled="form.id" v-model="form.departmentId" placeholder="请选择机构名称" style="width:100%;" @change="handleDepartChange">
                <el-option
                  v-for="item in departmentList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="片区名称" prop="parkId">
              <el-select v-model="form.parkId" placeholder="请选择片区名称" style="width:100%;" >
                <el-option
                  v-for="item in areaList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="泊位号" prop="code">
              <el-input v-model="form.code" placeholder="请输入泊位号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地磁编码" prop="magsensorCode">
              <el-input v-model="form.magsensorCode" placeholder="请输入地磁编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地磁厂家" prop="magsensorManufacturer">
              <el-select v-model="form.magsensorManufacturer" placeholder="请选择地磁厂家" clearable style="width:100%;">
                <el-option
                  v-for="dict in dict.type.EMagsensorManufacture"
                  :key="dict.value"
                  :label="dict.label"
                  :value="Number(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否推送巡检" prop="pushFlag">
              <el-radio-group v-model="form.pushFlag">
                <el-radio
                  v-for="dict in dict.type.EYesOrNo"
                  :key="dict.value"
                  :label="Number(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车位经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入车位经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车位纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入车位纬度"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button style="margin-left: 30px;" type="primary" @click="amapSelect">获取经纬度</el-button>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!form.id">
            <el-form-item label="是否隐藏" prop="hiddenFlag">
              <el-radio-group v-model="form.hiddenFlag">
                <el-radio
                  v-for="dict in dict.type.EYesOrNo"
                  :key="dict.value"
                  :label="Number(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="!form.id">
            <el-form-item label="是否下线" prop="offlineFlag">
              <el-radio-group v-model="form.offlineFlag">
                <el-radio
                  v-for="dict in dict.type.EYesOrNo"
                  :key="dict.value"
                  :label="Number(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <mapLog ref="mapLog" @mapSubmit="mapSubmit" isDetail :formType="{
      'longitude':'longitude',
      'latitude':'latitude',
      'details': 'address',
    }"></mapLog>

  </div>
</template>

<script>
import {fetchList, berthDetail, berthAdd, berthUpdate, berthShow, berthHidden,berthSetOnline,berthSetOffline} from "@/api/park/berth";
import { listOrgs } from '@/api/system/organization'
import mapLog from "@/components/mapLog";
import { getListAuthPark,getDepartmentList } from '@/api/base'
export default {
  name: "berth",
  dicts: ['EBerthStatus','EYesOrNo','EMagsensorManufacture',],
  components:{mapLog},
  data() {
    return {
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'片区名称',prop:'parkLotName',},
        {label:'泊位编码',prop:'code',},
        {label:'地磁编码',prop:'magsensorCode',},
        {label:'车位状态',prop:'status',},
        {label:'是否推送巡检',prop:'pushFlag',},
        {label:'是否异常',prop:'abnormalFlag',},
        {label:'是否隐藏',prop:'hiddenFlag',},
        {label:'是否下线',prop:'offlineFlag',},
        {label:'最新更新时间',prop:'lastStatusUpdateTime',},
        {label:'最新地磁心跳上报时间',prop:'lastHeartbeatTime',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        departmentId: [
          {required: true, message: "机构不能为空", trigger: "change"}
        ],
        parkId: [
          {required: true, message: "片区不能为空", trigger: "change"}
        ],
        code: [
          {required: true, message: "泊位号不能为空", trigger: "blur"}
        ],
        // magsensorCode: [
        //   {required: true, message: "地磁编码不能为空", trigger: "blur"}
        // ],
        // magsensorManufacturer: [
        //   {required: true, message: "地磁厂家不能为空", trigger: "change"}
        // ],
        pushFlag: [
          {required: true, message: "是否推送巡检不能为空", trigger: "change"}
        ],
        hiddenFlag: [
          {required: true, message: "是否隐藏不能为空", trigger: "change"}
        ],
        offlineFlag: [
          {required: true, message: "是否下线不能为空", trigger: "change"}
        ],
        longitude: [
          {required: true, message: "车位经度不能为空", trigger: "blur"}
        ],
        latitude: [
          {required: true, message: "车位纬度不能为空", trigger: "blur"}
        ]
      },
      departmentList: [],//机构数据
      areaList:[],//片区数据
    };
  },
  created() {
    this.getDepartmentList()
    this.getList();
  },
  methods: {
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async getListAuthPark(departmentId){//获取停车场
      let res = await getListAuthPark({departmentId,parkType:2})
      return res.data || []
    },
    async handleDepartChange(val) {//机构选择
      let form = {...this.form}
      form.parkId = ''
      this.form = {...form}
      if(!val) return this.areaList = []
      this.areaList = await this.getListAuthPark(val)
    },
    //地图打开
    amapSelect(){
      this.$refs.mapLog.mapLoad(this.form)
    },
    mapSubmit(form){//地图弹窗提交
      this.form = {...form}
    },
    //下线上线
    handleOnline(row) {
      let api = row.offlineFlag == 1 ? berthSetOnline : berthSetOffline
      this.$modal.confirm(`是否确认${row.offlineFlag == 1 ? '上线' : '下线'}该数据项？`).then(function() {
        return api(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('操作成功');
      }).catch(() => {});
    },
    //隐藏显示
    handleHide(row) {
      let api = row.hiddenFlag == 1 ? berthShow : berthHidden
      this.$modal.confirm(`是否确认${row.offlineFlag == 1 ? '显示' : '隐藏'}该数据项？`).then(function() {
        return api(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('操作成功');
      }).catch(() => {});
    },
    /** 查询车位信息列表 */
    getList() {
      this.loading = true;
      fetchList(this.queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        this.spparkplaceList = rows;
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {};
      this.open = true;
      this.title = "添加车位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pkParkplaceId = row.id
      berthDetail(pkParkplaceId).then(async response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车位信息";
        this.areaList = await this.getListAuthPark(this.form.departmentId)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            berthUpdate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            berthAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
